<template>
  <div class="mod-config">
    <el-form :inline="true">
      <el-form-item>
        <el-button type="primary" @refreshDataList="getDataList" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border style="width: 100%;" v-loading="dataListLoading">
      <el-table-column align="center" type="index" header-align="center" min-width="150" label="ID"></el-table-column>
      <el-table-column
        align="center"
        prop="moduleName"
        header-align="center"
        min-width="150"
        label="模块名称"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="modulePath"
        header-align="center"
        min-width="150"
        label="模块路径(h5使用)"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="moduleClass"
        header-align="center"
        min-width="150"
        label="模块class(android)"
      ></el-table-column>
      <el-table-column align="center" prop="sort" header-align="center" min-width="150" label="排序"></el-table-column>
      <el-table-column
        align="center"
        prop="params"
        header-align="center"
        min-width="150"
        label="参数"
      ></el-table-column>
      <el-table-column header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="delOrUpdateHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!--    弹窗，新增/修改-->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './module-add-or-update'

export default {
  name: 'notice',
  data() {
    return {
      dataForm: {
        keyWord: ''
      },
      dataList: [],
      dataListSelections: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      addOrUpdateVisible: false,
      editOrUpdateVisible: false,
      dataListLoading: false
    }
  },
  activated() {
    this.getDataList()
  },
  components: { AddOrUpdate },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: '/module/list',
        method: 'post',
        data: {
          page: this.pageIndex,
          limit: this.pageSize
        }
      }).then(data => {
        if (data && data.code === 200) {
          this.dataList = data.data.list
          this.totalPage = data.data.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 新增修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    delOrUpdateHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id
          })
      this.$confirm(
        `确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        this.$http({
          url: '/module/delete',
          method: 'post',
          data: { ids: ids.join(',') }
        }).then(data => {
          if (data && data.code === 200) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500
            })
            this.getDataList()
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    }
  }
}
</script>¬
